"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : list_range二级分区表:外表
Create At   : 2022/2/15
Owner       : @daiguatutu
Description :
    1.安装postgres_fdw扩展,创建外部服务器,用户映射,模式
    2.创建表空间和二级分区表
    3.设置autovacuum_enabled的值为false,插入数据
    4.创建外表并查询数据
    5.删除二级分区表指定数据,查询外表数据
    6.二级分区表插入数据,删除外表指定数据
    7.创建一级分区表
    8.设置autovacuum_enabled的值为false,插入数据
    9.创建外表并查询数据
    10.删除一级分区表指定数据,查询外表数据
    11.一级分区表插入数据,删除外表指定数据
    12.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.创建成功,查询外表数据合理报错
    5.删除成功,查询外表数据合理报错
    6.插入成功,删除外表数据合理报错
    7.成功
    8.成功
    9.创建成功,查询外表数据合理报错
    10.删除成功,查询外表数据合理报错
    11.插入成功,删除外表数据合理报错
    12.成功
History     :
"""
import os
import unittest
from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.primaryNode = Node('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0120'
        self.ts_name = 'ts_subpartition_0120'
        self.s_name = 's_subpartition_0120'
        self.fs_name = 'fs_subpartition_0120'

    def test_subpartition(self):
        text = '-----step1:安装postgres_fdw扩展,创建外部服务器,用户映射,模式, expect:成功-----'
        self.log.info(text)
        create_sql = f"drop extension if exists postgres_fdw cascade; " \
            f"create extension if not exists postgres_fdw; " \
            f"drop server if exists {self.fs_name} cascade; " \
            f"create server {self.fs_name} foreign data wrapper " \
            f"postgres_fdw options (dbname '{self.primaryNode.db_name}', " \
            f"port '{self.primaryNode.db_port}'); " \
            f"drop user mapping if exists for current_user " \
            f"server {self.fs_name}; " \
            f"create user mapping for current_user server {self.fs_name}; " \
            f"drop schema if exists {self.s_name} cascade; " \
            f"create schema {self.s_name}; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.create_extension_success, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.create_server_success, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.create_usermapping_success, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.drop_server_success, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.drop_extension_success, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, msg,
                      '执行失败:' + text)

        text = '-----step2:创建表空间和二级分区表 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';" \
            f"create table if not exists {self.s_name}.{self.t_name}(" \
            f"col_1 int ,col_2 int ,col_3 varchar2 ( 30 ) " \
            f"not null ,col_4 int)tablespace {self.ts_name} " \
            f"partition by list (col_1) subpartition by range (col_2)" \
            f"( partition p_list_1 values(-1,-2,-3,-4,-5,-6,-7,-8,-9,-10 )" \
            f"  ( subpartition p_range_1_1 values less than( -10 )," \
            f"    subpartition p_range_1_2 values less than( 0 )," \
            f"    subpartition p_range_1_3 values less than( 10 )," \
            f"    subpartition p_range_1_4 values less than( 20 )," \
            f"    subpartition p_range_1_5 values less than( 50 )" \
            f"  )," \
            f"  partition p_list_2 values(1,2,3,4,5,6,7,8,9,10 )," \
            f"  partition p_list_3 values(11,12,13,14,15,16,17,18,19,20)" \
            f"  ( subpartition p_range_3_1 values less than( 15 )," \
            f"    subpartition p_range_3_2 values less than( maxvalue ))," \
            f"  partition p_list_4 values(21,22,23,24,25,26,27,28,29,30)" \
            f"  ( subpartition p_range_4_1 values less than( -10 )," \
            f"    subpartition p_range_4_2 values less than( 0 )," \
            f"    subpartition p_range_4_3 values less than( 10 )," \
            f"    subpartition p_range_4_4 values less than( 20 )," \
            f"    subpartition p_range_4_5 values less than( 50 ))," \
            f"  partition p_list_5 values(31,32,33,34,35,36,37,38,39,40)" \
            f"  ( subpartition p_range_5_1 values less than( maxvalue ))," \
            f"  partition p_list_6 values(41,42,43,44,45,46,47,48,49,50)" \
            f"  ( subpartition p_range_6_1 values less than( -10 )," \
            f"    subpartition p_range_6_2 values less than( 0 )," \
            f"    subpartition p_range_6_3 values less than( 10 )," \
            f"    subpartition p_range_6_4 values less than( 20 )," \
            f"    subpartition p_range_6_5 values less than( 50 ))," \
            f"  partition p_list_7 values(default)" \
            f") enable row movement;" 
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg, '执行失败:' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step3:设置autovacuum_enabled的值为false,插入数据 expect:成功-----'
        self.log.info(text)
        sql_1 = f"alter table {self.s_name}.{self.t_name} " \
            f"set (autovacuum_enabled = 'false'); " \
            f"insert into  {self.s_name}.{self.t_name} " \
            f"values (generate_series(-19, 19),generate_series(-10, 49)," \
            f"generate_series(0, 99)); "
        msg = self.pri_sh.execut_db_sql(sql_1)
        self.log.info(msg)
        self.assertIn(self.constant.ALTER_TABLE_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step4:创建外表并查询数据 expect:创建成功,查询外表数据合理报错-----'
        self.log.info(text)
        sql_2 = f"drop foreign table if exists {self.t_name}_01; " \
            f"create foreign table {self.t_name}_01 ( " \
            f"col_1 int, col_2 int, col_3 varchar2 (30), col_4 int ) " \
            f"server {self.fs_name} options (schema_name '{self.s_name}', " \
            f"table_name '{self.t_name}'); " \
            f"select count(*) from {self.t_name}_01;"
        msg = self.pri_sh.execut_db_sql(sql_2)
        self.log.info(msg)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)

        text = '-----step5:删除二级分区表指定数据,查询外表数据 expect:删除成功,查询外表数据合理报错-----'
        self.log.info(text)
        sql_3 = f"delete {self.s_name}.{self.t_name} where col_1 =4; " \
            f"select count(*) from {self.t_name}_01;"
        msg = self.pri_sh.execut_db_sql(sql_3)
        self.log.info(msg)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)

        text = '-----step6:二级分区表插入数据,删除外表指定数据 expect:插入成功,删除外表数据合理报错-----'
        self.log.info(text)
        sql_4 = f"insert into {self.s_name}.{self.t_name} values(1,1,1,1);" \
            f"delete {self.t_name}_01 where col_1 =5;"
        msg = self.pri_sh.execut_db_sql(sql_4)
        self.log.info(msg)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step7:创建一级分区表 expect:成功-----'
        self.log.info(text)
        sql_5 = f"drop table if exists {self.s_name}.{self.t_name}; " \
            f"create table {self.s_name}.{self.t_name} " \
            f"(col_1 int, col_2 int, col_3 varchar2 (30), col_4 int " \
            f")tablespace {self.ts_name} partition by  hash (col_2) ( " \
            f"partition p_hash_1, partition p_hash_2, partition p_hash_3 " \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(sql_5)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step8:设置autovacuum_enabled的值为false,插入数据 expect:成功-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql_1)
        self.log.info(msg)
        self.assertIn(self.constant.ALTER_TABLE_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step9:创建外表并查询数据 expect:创建成功,查询外表数据合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql_2)
        self.log.info(msg)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)

        text = '-----step10:删除一级分区表指定数据,查询外表数据 expect:删除成功,查询外表数据合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql_3)
        self.log.info(msg)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)

        text = '-----step11:一级分区表插入数据,删除外表指定数据 expect:插入成功,删除外表数据合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql_4)
        self.log.info(msg)
        self.assertIn(self.constant.foreign_error_msg, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step12:清理环境-----')
        text_1 = '-----删除外部服务器,表,表空间,模式,卸载postgres_fdw扩展 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f"drop server if exists {self.fs_name} cascade; " \
            f"drop foreign table if exists {self.t_name}_01; " \
            f"drop table if exists {self.s_name}.{self.t_name}; " \
            f"drop tablespace if exists {self.ts_name}; " \
            f"drop extension if exists postgres_fdw cascade; " \
            f"drop schema if exists {self.s_name}; "
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.drop_server_success, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.drop_extension_success, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
